#!/usr/bin/env python
#-*- coding:utf-8 -*-

from nose.plugins import Plugin
from subprocess import call

def notify(icon, summary, body=""):
    call(["notify-send", '-i', icon, summary, body])

class NoseGnomeNotify (Plugin):

    def __init__(self):
        super(NoseGnomeNotify, self).__init__()
        self.number_of_failed_tests = 0
    
    def addError(self, test, err):
        self.record_failed_test(test)
    
    def addFailure(self, test, err):
        self.record_failed_test(test)
    
    def record_failed_test(self, test):
        self.number_of_failed_tests += 1
        self.failed_test = test 
        
    def finalize(self, result):
        if self.number_of_failed_tests == 0:
            notify("dialog-ok", "All tests passed")
        elif self.number_of_failed_tests == 1:
            notify("dialog-error", "One test failed", self.failed_test.id())
        else:
            notify("dialog-error", "Multiple tests failed")

